package com.moli.iov.gateway.util;

import com.moli.iov.gateway.server.task.UpStreamDataTask;
import com.moli.iov.util.ByteArrayUtil;
import com.moli.iov.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.concurrent.ConcurrentHashMap;

/**
 * byte转16进制
 * Description:
 *
 * @author zql
 * @date 2017年3月27日
 */
public class GatewayUtil {

    private static final Logger logger = LoggerFactory.getLogger(GatewayUtil.class);
    /**
     * 解析vin
     *
     * @param res
     * @return
     */
    public static String getVin(byte[] res) {
        if (res == null || res.length <= 0) {
            return null;
        }
        byte[] vin = new byte[17];
        try {
            System.arraycopy(res, 4, vin, 0, vin.length);
        } catch (Exception e) {
            return null;
        }
        return new String(vin);
    }

    /**
     * 获取tbox的iccid
     * @param res
     * @return
     */
    public static String getICCID(byte[] res) {
        if (res == null || res.length <= 42) {
            return null;
        }
        byte[] iccid = new byte[20];
        try {
            System.arraycopy(res, 32, iccid, 0, iccid.length);
        } catch (Exception e) {
            return null;
        }
        return new String(iccid);
    }

    /**
     * 转换short为byte
     *
     * @param dst
     * @param src   需要转换的short
     * @param index
     */
    public static void putByte(byte[] dst, byte src, int index) {
        dst[index] = (byte) (src >> 0);
    }

    /**
     * 通过byte数组取到byte
     *
     * @param dst
     * @param index 第几位开始取
     * @return
     */
    public static byte getByte(byte[] dst, int index) {
        return dst[index];
    }


    /**
     * 转换short为byte
     *
     * @param dst
     * @param src   需要转换的short
     * @param index
     */
    public static void putShort(byte[] dst, short src, int index) {
        dst[index] = (byte) (src >> 8);
        dst[index + 1] = (byte) (src >> 0);
    }

    /**
     * 通过byte数组取到short
     *
     * @param b
     * @param index 第几位开始取
     * @return
     */
    public static short getShort(byte[] b, int index) {
        return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));
    }

    /**
     * 转换int为byte数组
     *
     * @param bb
     * @param x
     * @param index
     */
    public static void putInt(byte[] bb, int x, int index) {
        bb[index + 0] = (byte) (x >> 24);
        bb[index + 1] = (byte) (x >> 16);
        bb[index + 2] = (byte) (x >> 8);
        bb[index + 3] = (byte) (x >> 0);
    }

    /**
     * 通过byte数组取到int
     *
     * @param bb
     * @param index 第几位开始
     * @return
     */
    public static int getInt(byte[] bb, int index) {
        return (int) ((((bb[index + 0] & 0xff) << 24)
                | ((bb[index + 1] & 0xff) << 16)
                | ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));
    }

    /**
     * 转换long型为byte数组
     *
     * @param bb
     * @param x
     * @param index
     */
    public static void putLong(byte[] bb, long x, int index) {
        bb[index + 0] = (byte) (x >> 56);
        bb[index + 1] = (byte) (x >> 48);
        bb[index + 2] = (byte) (x >> 40);
        bb[index + 3] = (byte) (x >> 32);
        bb[index + 4] = (byte) (x >> 24);
        bb[index + 5] = (byte) (x >> 16);
        bb[index + 6] = (byte) (x >> 8);
        bb[index + 7] = (byte) (x >> 0);
    }

    /**
     * 通过byte数组取到long
     *
     * @param bb
     * @param index
     * @return
     */
    public static long getLong(byte[] bb, int index) {
        return ((((long) bb[index + 0] & 0xff) << 56)
                | (((long) bb[index + 1] & 0xff) << 48)
                | (((long) bb[index + 2] & 0xff) << 40)
                | (((long) bb[index + 3] & 0xff) << 32)
                | (((long) bb[index + 4] & 0xff) << 24)
                | (((long) bb[index + 5] & 0xff) << 16)
                | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));
    }



    public static void main(String[] args) {
//        byte[] a = new byte[2];
//        putShort(a, (short) 40,0);
//        String s = ByteArrayUtil.toHexString(a);
//        System.out.println(s);

        String a = "232301fe4c4e425343423346384a5731383639383201001e13050f070e26000338393836303242343037313633303030303931380000e3";
        byte[] stringToBytes = ByteArrayUtil.hexStringToBytes(a);
        String iccid = getICCID(stringToBytes);
        System.out.println(iccid);
    }
}
